package com.smugmugsoftware.airconcalc.advanced;

import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class SensibleLoadDialog extends DialogFragment {

	public static final int APPLIANCE_WIZARD_SELECTION = 5999;
	public static final int COMMERCIAL_WIZARD_SELECTION = 6999;

	private MainActivityInterface activity;
	private Context context;
	private int type;
	private String subString;
	private String[] subStringArray;
	private boolean optionList;

	private SensibleLoadDialog thisDialog;

	public SensibleLoadDialog() {}

	public SensibleLoadDialog(Context context, int type) {
		this.context = context;
		this.type = type;
		this.thisDialog = this;
	}

	public SensibleLoadDialog(Context context, int type, String subString) {
		this.context = context;
		this.type = type;
		this.thisDialog = this;
		this.subString = subString;
	}

	public SensibleLoadDialog(Context context, int type, String[] subStringArray) {
		this.context = context;
		this.type = type;
		this.thisDialog = this;
		this.subStringArray = subStringArray;
		this.subString = subStringArray[subStringArray.length - 1];
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			this.activity = (MainActivityInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener");
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View thisView = null;

		if (type == COMMERCIAL_WIZARD_SELECTION) {

			TableParser parser = new TableParser(context, R.raw.sensible);
			String[] parserList = parser.getSubIndex(new String[] { "Commercial" });
			final String[] list = new String[parserList.length + 1];
			list[0] = "< CUSTOM >";
			for (int i = 0; i < parserList.length; i++) {
				list[i + 1] = parserList[i];
			}

			builder.setItems(list, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (which == 0) {
						thisDialog.dismiss();
						activity.displaySensibleLoadDialog(context, SensibleLoad.COMMERCIAL, "Custom");
					} else {
						thisDialog.dismiss();
						activity.displaySensibleLoadDialog(context, SensibleLoad.COMMERCIAL, list[which]);
					}
				}
			});
			return builder.create();
		}

		if (type == APPLIANCE_WIZARD_SELECTION) {

			TableParser parser = new TableParser(context, R.raw.sensible);
			final String[] parserList = parser.getSubIndex(new String[] { "Appliances" });

			builder.setItems(parserList, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					thisDialog.dismiss();
					activity.displaySensibleLoadDialog(context, SensibleLoad.APPLIANCE, parserList[which]);
				}
			});
			return builder.create();
		}

		if (type == SensibleLoad.LIGHTING) {
			builder.setTitle("Add Lighting");
			thisView = inflater.inflate(R.layout.bulb_selector_dialog, null);

			final RadioButton bulbButton = (RadioButton) thisView.findViewById(R.id.bulb_radio);
			final RadioButton cflButton = (RadioButton) thisView.findViewById(R.id.cfl_radio);
			final RadioButton ledButton = (RadioButton) thisView.findViewById(R.id.led_radio);

			final LinearLayout bulbWrapper = (LinearLayout) thisView.findViewById(R.id.bulbWrapper);
			final LinearLayout cflWrapper = (LinearLayout) thisView.findViewById(R.id.cflWrapper);
			final LinearLayout ledWrapper = (LinearLayout) thisView.findViewById(R.id.ledWrapper);

			bulbWrapper.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					bulbButton.setChecked(true);
				}
			});

			cflWrapper.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					cflButton.setChecked(true);
				}
			});

			ledWrapper.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					ledButton.setChecked(true);
				}
			});

			bulbButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						cflButton.setChecked(false);
						ledButton.setChecked(false);
					}
				}
			});

			cflButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						bulbButton.setChecked(false);
						ledButton.setChecked(false);
					}
				}
			});

			ledButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						bulbButton.setChecked(false);
						cflButton.setChecked(false);
					}
				}
			});
		} else if (type == SensibleLoad.OCCUPANT) {
			builder.setTitle("Add Occupants");
			thisView = inflater.inflate(R.layout.occupant_selector_dialog, null);

			Spinner occupantSpinner = (Spinner) thisView.findViewById(R.id.occupant_spinner);
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			TableParser parser = new TableParser(context, R.raw.sensible);
			String[] facingString = parser.getSubIndex(new String[] { "Occupants", "Occupants", "List" });
			for (int i = 0; i < facingString.length; i++) {
				adapter.add(facingString[i]);
			}
			occupantSpinner.setAdapter(adapter);
			occupantSpinner.setSelection(0);

		} else if (type == SensibleLoad.APPLIANCE) {
			builder.setTitle("Add Appliances");

			if (subString.equals("Computer Equipment")) {

				if (subStringArray == null) {
					subStringArray = new String[] { "Appliances", "Computer Equipment" };
				}

				TableParser parser = new TableParser(context, R.raw.sensible);
				final String[] parserList = parser.getSubIndex(subStringArray);

				builder.setItems(parserList, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						thisDialog.dismiss();
						String[] newString = new String[subStringArray.length + 1];
						for (int i = 0; i < subStringArray.length; i++) {
							newString[i] = subStringArray[i];
						}
						newString[newString.length - 1] = parserList[which];
						thisDialog.dismiss();
						activity.displaySensibleLoadDialog(context, SensibleLoad.APPLIANCE, newString);
					}
				});
				return builder.create();

			} else if (subString.equals("Kitchen")) {
				View kitchenView = inflater.inflate(R.layout.kitchen_chooser_dialog, null);

				final LinearLayout headingWrapper = (LinearLayout) kitchenView.findViewById(R.id.explainThisHeadingWrapper);
				final TextView plusMinus = (TextView) kitchenView.findViewById(R.id.explainThisExpander);
				final TextView moreLess = (TextView) kitchenView.findViewById(R.id.explainThisLabel);
				final TextView help = (TextView) kitchenView.findViewById(R.id.help);

				plusMinus.setText("+");
				moreLess.setText("More Info");
				help.setVisibility(View.GONE);

				final TextView usagePercent = (TextView) kitchenView.findViewById(R.id.use_percent);
				final SeekBar usageBar = (SeekBar) kitchenView.findViewById(R.id.usage_seekBar);
				usageBar.setMax(100);
				usageBar.setProgress(50);
				usagePercent.setText("50");
				usageBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
					public void onStopTrackingTouch(SeekBar seekBar) {}				
					public void onStartTrackingTouch(SeekBar seekBar) {}				
					public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
						usagePercent.setText(String.valueOf(progress));
					}
				});


				headingWrapper.setOnClickListener(new android.view.View.OnClickListener() {
					public void onClick(View v) {
						if (plusMinus.getText().equals("+")) {
							plusMinus.setText("-");
							moreLess.setText("Less Info");
							help.setVisibility(View.VISIBLE);
						} else {
							plusMinus.setText("+");
							moreLess.setText("More Info");
							help.setVisibility(View.GONE);
						}
					}
				});


				final EditText rating = (EditText) kitchenView.findViewById(R.id.watts);
				final EditText quantity = (EditText) kitchenView.findViewById(R.id.qty);
				Button kitchenButton = (Button) kitchenView.findViewById(R.id.kitchen_button);
				kitchenButton.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						String[] returnList = new String[2];
						double[] data = new double[2];

						data[0] = 1;
						data[1] = 1200;

						returnList[0] = "AK-b1200";
						returnList[1] = "Std. Kitchen Load";

						thisDialog.dismiss();				
						activity.getDataFromDialog(SensibleLoad.APPLIANCE_KITCHEN_BTU, data, returnList);
					}
				});

				builder.setView(kitchenView);

				builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						thisDialog.dismiss();				
					}
				});

				builder.setNeutralButton("BTU-Rated", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						String[] returnList = new String[2];
						double[] data = new double[2];

						data[0] = zeroCheck(quantity);
						data[1] = zeroCheck(rating);

						if (data[0] == 0 || data[1] == 0) {
							return;
						}

						returnList[0] = "AK-b" + String.valueOf((int) data[1]);
						returnList[1] = "Kitchen: " + String.valueOf((int) data[1]) + " BTU";

						data[1] = data[1] * 0.3 * (double) ((double) usageBar.getProgress() / 100);
						thisDialog.dismiss();		
						activity.getDataFromDialog(SensibleLoad.APPLIANCE_KITCHEN_BTU, data, returnList);
					}
				});

				builder.setPositiveButton("Watt-Rated", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						String[] returnList = new String[3];
						double[] data = new double[3];

						data[0] = zeroCheck(quantity);
						data[1] = zeroCheck(rating);
						data[2] = 30;

						if (data[0] == 0 || data[1] == 0) {
							return;
						}

						returnList[0] = "AK-w" + String.valueOf((int) data[1]);
						returnList[1] = "Kitchen: " + String.valueOf((int) data[1]) + " W";

						data[1] = data[1] * (double) ((double) usageBar.getProgress() / 100);
						thisDialog.dismiss();				
						activity.getDataFromDialog(SensibleLoad.APPLIANCE_KITCHEN_WATT, data, returnList);
					}
				});

				return builder.create();

			} else if (subString.equals("Fireplace")) {
				View fireplaceView = inflater.inflate(R.layout.fireplace_chooser_dialog, null);

				final EditText quantity = (EditText) fireplaceView.findViewById(R.id.qty);
				final RadioGroup fireplaceGroup = (RadioGroup) fireplaceView.findViewById(R.id.fireplace_radio_group);

				builder.setView(fireplaceView);

				builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						thisDialog.dismiss();				
					}
				});

				builder.setPositiveButton("Watt-Rated", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						String[] returnList = new String[2];
						double[] data = new double[1];

						data[0] = zeroCheck(quantity);

						if (data[0] == 0) {
							return;
						}
						
						returnList[0] = "AF-";

						if (fireplaceGroup.getCheckedRadioButtonId() == R.id.fireplace_best) {
							returnList[0] = returnList[0] + "b";
						} else if (fireplaceGroup.getCheckedRadioButtonId() == R.id.fireplace_average) {
							returnList[0] = returnList[0] + "a";
						} else if (fireplaceGroup.getCheckedRadioButtonId() == R.id.fireplace_poor) {
							returnList[0] = returnList[0] + "p";					
						}
						
						returnList[1] = "Fireplace";

						thisDialog.dismiss();				
						activity.getDataFromDialog(SensibleLoad.APPLIANCE_FIREPLACE, data, returnList);
					}
				});

				return builder.create();
			}

			thisView = inflater.inflate(R.layout.load_selector_dialog, null);

			RelativeLayout spinnerWrapper = (RelativeLayout) thisView.findViewById(R.id.spinnerWrapper);
			RelativeLayout wattageWrapper = (RelativeLayout) thisView.findViewById(R.id.wattageWrapper);
			LinearLayout efficiencyWrapper = (LinearLayout) thisView.findViewById(R.id.efficiencyWrapper);

			final TextView efficiencyLabel= (TextView) thisView.findViewById(R.id.efficiency_label_2);
			SeekBar efficiencyBar = (SeekBar) thisView.findViewById(R.id.efficiency_seekBar);
			efficiencyLabel.setText("Cool/Room Temperature");
			efficiencyBar.setMax(11);
			efficiencyBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				public void onStopTrackingTouch(SeekBar seekBar) {}				
				public void onStartTrackingTouch(SeekBar seekBar) {}				
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					if (progress < 2) {
						efficiencyLabel.setText("Cool/Room Temperature");
					} else if (progress >= 2 && progress < 4) {
						efficiencyLabel.setText("Above Room Temperature");
					} else if (progress >= 4 && progress < 6) {
						efficiencyLabel.setText("Warm");
					} else if (progress >= 6 && progress < 8) {
						efficiencyLabel.setText("Hot to the Touch");
					} else if (progress >= 8 && progress < 10) {
						efficiencyLabel.setText("Too Hot to Touch");
					} else if (progress >= 10 && progress <= 11) {
						efficiencyLabel.setText("Scalding");
					}
				}
			});

			final TextView usagePercent = (TextView) thisView.findViewById(R.id.use_percent);
			SeekBar usageBar = (SeekBar) thisView.findViewById(R.id.usage_seekBar);
			usageBar.setMax(100);
			usageBar.setProgress(50);
			usagePercent.setText("50");
			usageBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				public void onStopTrackingTouch(SeekBar seekBar) {}				
				public void onStartTrackingTouch(SeekBar seekBar) {}				
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					usagePercent.setText(String.valueOf(progress));
				}
			});

			if (subString.equals("Appliances/Electronics")) {
				spinnerWrapper.setVisibility(View.GONE);
				wattageWrapper.setVisibility(View.VISIBLE);
				efficiencyWrapper.setVisibility(View.VISIBLE);

			} else {
				spinnerWrapper.setVisibility(View.VISIBLE);
				wattageWrapper.setVisibility(View.GONE);
				efficiencyWrapper.setVisibility(View.GONE);

				ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
				adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				TableParser parser = new TableParser(context, R.raw.sensible);
				if (subStringArray != null) {
					String[] parserList = parser.getSubIndex(subStringArray);

					for (int i = 0; i < parserList.length; i++) {
						adapter.add(parserList[i]);
					}
				} else {
					String[] stringArray = parser.getSubIndex(new String[] { "Appliances", subString });
					for (int i = 0; i < stringArray.length; i++) {
						adapter.add(stringArray[i]);
					}
				}

				Spinner loadSpinner = (Spinner) thisView.findViewById(R.id.load_spinner);
				loadSpinner.setAdapter(adapter);
			}

		} else if (type == SensibleLoad.COMMERCIAL) {
			builder.setTitle("Add Commercial Equipment");
			thisView = inflater.inflate(R.layout.load_selector_dialog, null);

			RelativeLayout spinnerWrapper = (RelativeLayout) thisView.findViewById(R.id.spinnerWrapper);
			RelativeLayout wattageWrapper = (RelativeLayout) thisView.findViewById(R.id.wattageWrapper);
			LinearLayout efficiencyWrapper = (LinearLayout) thisView.findViewById(R.id.efficiencyWrapper);

			final TextView efficiencyLabel= (TextView) thisView.findViewById(R.id.efficiency_label);
			final SeekBar efficiencyBar = (SeekBar) thisView.findViewById(R.id.efficiency_seekBar);

			final TextView usagePercent = (TextView) thisView.findViewById(R.id.use_percent);
			final SeekBar usageBar = (SeekBar) thisView.findViewById(R.id.usage_seekBar);
			usageBar.setMax(100);
			usageBar.setProgress(50);
			usagePercent.setText("50");
			usageBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				public void onStopTrackingTouch(SeekBar seekBar) {}				
				public void onStartTrackingTouch(SeekBar seekBar) {}				
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					usagePercent.setText(String.valueOf(progress));
				}
			});

			if (subString.equals("Custom")) {

				((TextView) thisView.findViewById(R.id.watts_unit_label)).setVisibility(View.GONE);
				spinnerWrapper.setVisibility(View.GONE);
				final EditText rating = (EditText) thisView.findViewById(R.id.watts);
				final EditText quantity = (EditText) thisView.findViewById(R.id.qty);

				((TextView) thisView.findViewById(R.id.efficiency_label_2)).setText("25% is most accurate for " +
						"equipment without a large radiant output (ex: lights/heating devices)");

				efficiencyLabel.setText("Radiant Efficiency: 25%");
				efficiencyBar.setMax(35);
				efficiencyBar.setProgress(0);
				efficiencyBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
					public void onStopTrackingTouch(SeekBar seekBar) {}				
					public void onStartTrackingTouch(SeekBar seekBar) {}				
					public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
						efficiencyLabel.setText("Radiant Efficiency: " + String.valueOf(progress + 25) + "%");
					}
				});

				builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						thisDialog.dismiss();				
					}
				});

				builder.setNeutralButton("BTU-Rated", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						double[] returnList = new double[2];
						String[] idStrings = new String[2];

						returnList[0] = zeroCheck(quantity);
						returnList[1] = zeroCheck(rating);

						if (returnList[0] == 0 || returnList[1] == 0) {
							return;						
						}

						idStrings[0] = "CC-b" + String.valueOf((int) returnList[1]);
						idStrings[1] = "Commercial";

						returnList[1] = returnList[1] * (double) ((double) usageBar.getProgress() / 100)
								* (double) ((double) (efficiencyBar.getProgress() + 25) / 100);

						activity.getDataFromDialog(SensibleLoad.COMMERCIAL_CUSTOM_BTU, returnList, idStrings);
					}
				});

				builder.setPositiveButton("Watt-Rated", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						double[] returnList = new double[3];
						String[] idStrings = new String[2];

						returnList[0] = zeroCheck(quantity);
						returnList[1] = zeroCheck(rating);
						returnList[2] = (double) usageBar.getProgress();

						if (returnList[0] == 0 || returnList[1] == 0) {
							return;						
						}

						idStrings[0] = "CC-w" + String.valueOf((int) returnList[1]);
						idStrings[1] = "Commercial";

						returnList[1] = returnList[1] * (double) ((double) (efficiencyBar.getProgress() + 25) / 100);

						activity.getDataFromDialog(SensibleLoad.COMMERCIAL_CUSTOM_WATT, returnList, idStrings);
					}
				});

				builder.setView(thisView);
				return builder.create();
			}

			efficiencyLabel.setText("Cool/Room Temperature");
			efficiencyBar.setMax(11);
			efficiencyBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				public void onStopTrackingTouch(SeekBar seekBar) {}				
				public void onStartTrackingTouch(SeekBar seekBar) {}				
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					if (progress < 2) {
						efficiencyLabel.setText("Cool/Room Temperature");
					} else if (progress >= 2 && progress < 4) {
						efficiencyLabel.setText("Above Room Temperature");
					} else if (progress >= 4 && progress < 6) {
						efficiencyLabel.setText("Warm");
					} else if (progress >= 6 && progress < 8) {
						efficiencyLabel.setText("Hot to the Touch");
					} else if (progress >= 8 && progress < 10) {
						efficiencyLabel.setText("Too Hot to Touch");
					} else if (progress >= 10 && progress <= 11) {
						efficiencyLabel.setText("Scalding");
					}
				}
			});

			spinnerWrapper.setVisibility(View.VISIBLE);
			wattageWrapper.setVisibility(View.GONE);
			efficiencyWrapper.setVisibility(View.GONE);

			ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			TableParser parser = new TableParser(context, R.raw.sensible);
			String[] stringArray = parser.getSubIndex(new String[] { "Commercial", subString, "List" });
			for (int i = 0; i < stringArray.length; i++) {
				adapter.add(stringArray[i]);
			}

			Spinner loadSpinner = (Spinner) thisView.findViewById(R.id.load_spinner);
			loadSpinner.setAdapter(adapter);
		}

		builder.setView(thisView);

		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				thisDialog.dismiss();				
			}
		});

		builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

				if (type == SensibleLoad.LIGHTING) {
					EditText watts = (EditText) thisDialog.getDialog().findViewById(R.id.wattage);
					EditText qty = (EditText) thisDialog.getDialog().findViewById(R.id.qty);
					RadioButton bulbButton = (RadioButton) thisDialog.getDialog().findViewById(R.id.bulb_radio);
					RadioButton cflButton = (RadioButton) thisDialog.getDialog().findViewById(R.id.cfl_radio);
					RadioButton ledButton = (RadioButton) thisDialog.getDialog().findViewById(R.id.led_radio);

					int bulbType = 0;

					double[] returnList = new double[3];
					returnList[0] = zeroCheck(watts);
					returnList[2] = zeroCheck(qty);

					if (returnList[0] == 0 || returnList[2] == 0) {
						return;						
					}

					if (bulbButton.isChecked()) {
						bulbType = SensibleLoad.LIGHT_BULB;
						returnList[1] = 70;
					} else if (cflButton.isChecked()) {
						bulbType = SensibleLoad.LIGHT_CFL;
						returnList[1] = 35;
					} else if (ledButton.isChecked()) {
						bulbType = SensibleLoad.LIGHT_LED;
						returnList[1] = 40;
					}
					activity.getDataFromDialog(type, returnList, bulbType);

				} else if (type == SensibleLoad.OCCUPANT) {
					EditText qty = (EditText) thisDialog.getDialog().findViewById(R.id.qty);
					Spinner occupantType = (Spinner) thisDialog.getDialog().findViewById(R.id.occupant_spinner);

					String thisOccupant = occupantType.getSelectedItem().toString();

					TableParser parser = new TableParser(context, R.raw.sensible);

					String[] returnList = new String[3];

					returnList[0] = parser.getItemNumber(new String[] { "Occupants", "Occupants", thisOccupant });
					returnList[1] = parser.getDataPoint(returnList[0], "Occupants", 1);
					returnList[2] = parser.getDataPoint(returnList[0], "Occupants", 2);

					double[] data = new double[2];
					data[0] = zeroCheck(qty);
					data[1] = zeroCheck(returnList[2]);

					if (data[0] == 0 || data[1] == 0) {
						return;
					}
					activity.getDataFromDialog(type, data, returnList);

				} else if (type == SensibleLoad.APPLIANCE) {
					EditText watts = (EditText) thisDialog.getDialog().findViewById(R.id.watts);
					EditText qty = (EditText) thisDialog.getDialog().findViewById(R.id.qty);
					Spinner loadSpinner = (Spinner) thisDialog.getDialog().findViewById(R.id.load_spinner);
					SeekBar efficiencyBar = (SeekBar) thisDialog.getDialog().findViewById(R.id.efficiency_seekBar);
					SeekBar usageBar = (SeekBar) thisDialog.getDialog().findViewById(R.id.usage_seekBar);

					TableParser parser = new TableParser(context, R.raw.sensible);

					if (subString.equals("Appliances/Electronics")) {
						String[] returnList = new String[3];
						double[] data = new double[3];

						data[0] = zeroCheck(qty);
						data[1] = zeroCheck(watts);
						data[2] = (double) ((((double) efficiencyBar.getProgress() * 2.3) + 25) * (double) usageBar.getProgress()) / 100;

						returnList[0] = "AE-" + String.valueOf((int) data[1]);
						returnList[1] = "Appliance";
						returnList[2] = String.valueOf(data[1]);

						if (data[0] == 0 || data[1] == 0 || data[2] == 0) {
							return;
						}

						activity.getDataFromDialog(type, data, returnList);

					} else {
						String[] returnList = new String[3];
						double[] data = new double[3];
						double wattMultiplier = 1;

						String thisAppliance = loadSpinner.getSelectedItem().toString();

						if (subStringArray != null) {
							String[] newStringArray = new String[subStringArray.length + 1];
							wattMultiplier = 0.3;

							for (int i = 0; i < subStringArray.length; i++) {
								newStringArray[i] = subStringArray[i];
							}
							newStringArray[newStringArray.length - 1] = thisAppliance;

							returnList[0] = parser.getItemNumber(newStringArray);
						} else {
							returnList[0] = parser.getItemNumber(new String[] { "Appliances", subString, thisAppliance });
						}

						returnList[1] = parser.getDataPoint(returnList[0], "Appliances", 1);
						returnList[2] = parser.getDataPoint(returnList[0], "Appliances", 2);

						data[0] = zeroCheck(qty);
						data[1] = zeroCheck(returnList[2]) * wattMultiplier;
						data[2] = usageBar.getProgress();

						if (data[0] == 0 || data[2] == 0) {
							return;
						}

						activity.getDataFromDialog(type, data, returnList);
					}

				} else if (type == SensibleLoad.COMMERCIAL) {
					EditText qty = (EditText) thisDialog.getDialog().findViewById(R.id.qty);
					Spinner loadSpinner = (Spinner) thisDialog.getDialog().findViewById(R.id.load_spinner);
					SeekBar usageBar = (SeekBar) thisDialog.getDialog().findViewById(R.id.usage_seekBar);

					TableParser parser = new TableParser(context, R.raw.sensible);

					String[] returnList = new String[3];
					double[] data = new double[3];

					String thisAppliance = loadSpinner.getSelectedItem().toString();

					returnList[0] = parser.getItemNumber(new String[] { "Commercial", subString, thisAppliance });
					returnList[1] = parser.getDataPoint(returnList[0], "Commercial", 1);
					returnList[2] = parser.getDataPoint(returnList[0], "Commercial", 2);

					data[0] = zeroCheck(qty);
					data[1] = zeroCheck(returnList[2]);
					data[2] = usageBar.getProgress();

					if (data[0] == 0 || data[1] == 0) {
						return;
					}

					activity.getDataFromDialog(type, data, returnList);
				}

				thisDialog.dismiss();
			}
		});

		return builder.create();
	}

	protected int zeroCheck(EditText e) {
		String reader = e.getText().toString();
		if (reader == null || reader.length() == 0 || Integer.valueOf(reader) == 0) {
			return 0;
		} else {
			return Integer.parseInt(reader.toString());
		}
	}
	protected int zeroCheck(String s) {
		if (s == null || s.length() == 0 || Integer.valueOf(s) == 0) {
			return 0;
		} else {
			return Integer.parseInt(s.toString());
		}
	}
}
